//
//  NSString+Custom.m
//  FashionTech
//
//  Created by JackieYip on 16/5/4.
//  Copyright © 2016年 mondial-d. All rights reserved.
//

#import "NSString+Custom.h"
#import "FTMacro_Font.h"
#import "FTInternationalization_Macro.h"

@implementation NSString (Custom)
+ (NSString *)JSStringWithString:(NSString *)string lightGrayColorFont:(UIFont *)font
{
    NSString *JSString = [NSString stringWithFormat:
                          @"<html>\n"
                          "<head>\n"
                          "<style type =\"text/css\">\n"
                          "body{font-size:%.0f}\n"
                          "</style>\n"
                          "</head>\n"
                          "<body style = \"color:#AAAAAA\">%@</body>\n"
                          "</html>", font.pointSize, string];
    return JSString;
}

+ (NSString *)getLocalDateStringFormateUTCDateString:(NSString *)utcDateString
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //输入格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
    NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
    [dateFormatter setTimeZone:localTimeZone];
    
    NSDate *dateFormatted = [dateFormatter dateFromString:utcDateString];
    //输出格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
    return dateString;
}

- (CGSize)calculateSize:(CGSize)size font:(UIFont *)font
{
    CGSize expectedLabelSize = CGSizeZero;
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
    style.lineBreakMode = NSLineBreakByWordWrapping;
    style.lineHeightMultiple = 1.0;
    NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:style.copy};
    expectedLabelSize = [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    return CGSizeMake(ceil(expectedLabelSize.width), ceil(expectedLabelSize.height));
    
}

- (CGFloat)calculateLabelHeightWithSize:(CGSize)size font:(UIFont *)font
{
    if (!font) font = kFont14;
    // 单行高度
    CGFloat sigleLineHeight = [FTLocalizedStringInLocalizable(@"单行高度") calculateSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) font:font].height;
    // 超过一行的高度
    CGFloat labelHeight = 0;
    if (self.length > 0) {
        labelHeight = [self calculateSize:size font:font].height;
        if (labelHeight > sigleLineHeight) {
            labelHeight = labelHeight - sigleLineHeight;
        }
        else
            labelHeight = sigleLineHeight;
    }
    return labelHeight;
}
@end
